﻿using Hx.ADSyncPlatform.ActiveDirectory.Attributes;
using System;
using System.Collections.Generic;

namespace Hx.ADSyncPlatform.ActiveDirectory.Model
{
    public class DirectoryObject
    {
        /// <summary>
        ///构造方法
        /// </summary>
        public DirectoryObject()
        {
            AssignedAttributes = new List<string>();
        }

        private string cn;
        /// <summary>
        ///CN
        /// </summary>
        [IgnoreUpdate]
        [RequiredForCreate]
        public string CN
        {
            get
            {
                return cn;
            }
            set
            {
                cn = value;
                AssignedAttributes.Add(nameof(CN));
            }
        }

        private string name;
        /// <summary>
        /// 名称
        /// </summary>      
        [IgnoreUpdate]
        [RequiredForCreate]
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                AssignedAttributes.Add(nameof(Name));
            }
        }

        /// <summary>
        ///描述
        /// </summary>
        private string description;
        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
                AssignedAttributes.Add(nameof(Description));
            }
        }

        /// <summary>
        /// 显示名
        /// </summary>
        private string displayName;
        public string DisplayName
        {
            get
            {
                return displayName;
            }
            set
            {
                displayName = value;
                AssignedAttributes.Add(nameof(DisplayName));
            }
        }

        /// <summary>
        /// DN
        /// </summary> 
        private string distinguishedName;
        [ReadOnlyCustom]
        public string DistinguishedName
        {
            get
            {
                return distinguishedName;
            }
            set
            {
                distinguishedName = value;
                AssignedAttributes.Add(nameof(DistinguishedName));
            }
        }

        /// <summary>
        /// 类型
        /// </summary> 
        private string objectCategory;
        [ReadOnlyCustom]
        public string ObjectCategory
        {
            get
            {
                return objectCategory;
            }
            set
            {
                objectCategory = value;
                AssignedAttributes.Add(nameof(ObjectCategory));
            }
        }

        /// <summary>
        /// 类型
        /// </summary>  
        private string objectClass;
        [ReadOnlyCustom]
        public string ObjectClass
        {
            get
            {
                return objectClass;
            }
            set
            {
                objectClass = value;
                AssignedAttributes.Add(nameof(ObjectClass));
            }
        }

        /// <summary>
        /// 对象GUID
        /// </summary>
        private byte[] objectGUID;
        [ReadOnlyCustom]
        public byte[] ObjectGUID
        {
            get
            {
                return objectGUID;
            }
            set
            {
                objectGUID = value;
                AssignedAttributes.Add(nameof(ObjectGUID));
            }
        }

        /// <summary>
        /// 修改时间
        /// </summary>   
        private DateTime whenChanged;
        [ReadOnlyCustom]
        public DateTime WhenChanged
        {
            get
            {
                return whenChanged;
            }
            set
            {
                whenChanged = value;
                AssignedAttributes.Add(nameof(WhenChanged));
            }
        }

        /// <summary>
        ///创建时间
        /// </summary>
        private DateTime whenCreated;
        [ReadOnlyCustom]
        public DateTime WhenCreated
        {
            get
            {
                return whenCreated;
            }
            set
            {
                whenCreated = value;
                AssignedAttributes.Add(nameof(WhenCreated));
            }
        }

        /// <summary>
        /// 已赋值的属性集合
        /// </summary>
        [Ignore]
        internal List<string> AssignedAttributes
        {
            get;
            set;
        }
        /// <summary>
        /// 字符串Guid
        /// </summary>  
        private Guid cObjectGUID;
        [ReadOnlyCustom]
        public Guid CObjectGUID
        {
            get
            {
                return objectGUID != null ? new Guid(objectGUID) : Guid.Empty;
            }

        }

        //public override bool Equals(object obj)
        //{
        //    var data = (DirectoryObject)obj;
        //    return data.DistinguishedName == this.DistinguishedName;
        //}

        //public override int GetHashCode()
        //{
        //    return this.DistinguishedName.GetHashCode();
        //}
    }

    public class DirectoryObjectComparerr : IEqualityComparer<DirectoryObject>
    {
     
        public bool Equals(DirectoryObject x, DirectoryObject y)
        {
            if (x == null || y == null)
                return false;
            if (x.DistinguishedName == y.DistinguishedName)
                return true;
            else
                return false;
        }

    

        public int GetHashCode(DirectoryObject obj)
        {
            if (obj == null)
                return 0;
            else
                return obj.DistinguishedName.GetHashCode();
        }
    }

}
